package gui;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SpringLayout;

import model.Role;
import model.User;
import controller.Controller;


/**
 * A Panel to show the available options the logged in user can perform.
 * 
 * @author James Gunter - jpgunter
 *
 */
public class UserPanel extends JPanel implements ActionListener {
  
  private static final int PREFFERED_WIDTH = 200;
  private static final int PREFERRED_HEIGHT = 160;
  User my_user;
  MainWindow my_parent;
  private JButton my_view_all_jobs_btn;
  private SpringLayout my_spring_layout;
  private JButton my_view_my_jobs_btn;
  private JButton my_logout_btn;
  private JButton my_submit_job_btn;
  private JButton my_view_calendar_btn;
  private JButton my_view_volunteers_btn;
  private JButton my_view_blackballed_btn;
  private JButton my_view_pending_jobs_btn;
  
  public UserPanel(MainWindow the_parent, User the_user){
    my_parent = the_parent;
    my_user = the_user;

    setPreferredSize(new Dimension(PREFFERED_WIDTH, PREFERRED_HEIGHT));
    
    my_spring_layout = new SpringLayout();
    setLayout(my_spring_layout);
    
    
    
    switch(my_user.getUserRole()){
    case VOLUNTEER:
      addVolunteerButtons();
      break;
    case PARK_MANAGER:
      addParkManagerButtons();
      break;
    case STAFF_MEMBER:
      addStaffMemberButtons();
      break;
    }
  }
  
  private void addStaffMemberButtons() {
    my_view_calendar_btn = new JButton("View Calendar");
    my_view_calendar_btn.addActionListener(this);
    my_spring_layout.putConstraint(SpringLayout.NORTH, my_view_calendar_btn, 10, SpringLayout.NORTH, this);
    my_spring_layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, my_view_calendar_btn, 0, SpringLayout.HORIZONTAL_CENTER, this);
    add(my_view_calendar_btn);
    
    my_view_volunteers_btn = new JButton("View Volunteers");
    my_view_volunteers_btn.addActionListener(this);
    my_spring_layout.putConstraint(SpringLayout.NORTH, my_view_volunteers_btn, 10, SpringLayout.SOUTH, my_view_calendar_btn);
    my_spring_layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, my_view_volunteers_btn, 0, SpringLayout.HORIZONTAL_CENTER, this);
    add(my_view_volunteers_btn);
    
    my_view_blackballed_btn = new JButton("View Blackballed");
    my_view_blackballed_btn.addActionListener(this);
    my_spring_layout.putConstraint(SpringLayout.NORTH, my_view_blackballed_btn, 10, SpringLayout.SOUTH, my_view_volunteers_btn);
    my_spring_layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, my_view_blackballed_btn, 0, SpringLayout.HORIZONTAL_CENTER, this);
    add(my_view_blackballed_btn);
    
    my_logout_btn = new JButton("Logout");
    my_logout_btn.addActionListener(this);
    my_spring_layout.putConstraint(SpringLayout.NORTH, my_logout_btn, 10, SpringLayout.SOUTH, my_view_blackballed_btn);
    my_spring_layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, my_logout_btn, 0, SpringLayout.HORIZONTAL_CENTER, this);
    add(my_logout_btn);
  }

  private void addParkManagerButtons() {
    my_submit_job_btn = new JButton("Submit Job Request");
    my_submit_job_btn.addActionListener(this);
    my_spring_layout.putConstraint(SpringLayout.NORTH, my_submit_job_btn, 10, SpringLayout.NORTH, this);
    my_spring_layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, my_submit_job_btn, 0, SpringLayout.HORIZONTAL_CENTER, this);
    add(my_submit_job_btn);
    
    my_view_my_jobs_btn = new JButton("View My Jobs");
    my_view_my_jobs_btn.addActionListener(this);
    my_spring_layout.putConstraint(SpringLayout.NORTH, my_view_my_jobs_btn, 10, SpringLayout.SOUTH, my_submit_job_btn);
    my_spring_layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, my_view_my_jobs_btn, 0, SpringLayout.HORIZONTAL_CENTER, this);
    add(my_view_my_jobs_btn);
    
    my_logout_btn = new JButton("Logout");
    my_logout_btn.addActionListener(this);
    my_spring_layout.putConstraint(SpringLayout.NORTH, my_logout_btn, 10, SpringLayout.SOUTH, my_view_my_jobs_btn);
    my_spring_layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, my_logout_btn, 0, SpringLayout.HORIZONTAL_CENTER, this);
    add(my_logout_btn);
  }

  private void addVolunteerButtons() {
    my_view_pending_jobs_btn = new JButton("View All Jobs");
    my_view_pending_jobs_btn.addActionListener(this);
    my_spring_layout.putConstraint(SpringLayout.NORTH, my_view_pending_jobs_btn, 10, SpringLayout.NORTH, this);
    my_spring_layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, my_view_pending_jobs_btn, 0, SpringLayout.HORIZONTAL_CENTER, this);
    add(my_view_pending_jobs_btn);
    
    my_view_my_jobs_btn = new JButton("View My Jobs");
    my_view_my_jobs_btn.addActionListener(this);
    my_spring_layout.putConstraint(SpringLayout.NORTH, my_view_my_jobs_btn, 10, SpringLayout.SOUTH, my_view_pending_jobs_btn);
    my_spring_layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, my_view_my_jobs_btn, 0, SpringLayout.HORIZONTAL_CENTER, this);
    add(my_view_my_jobs_btn);
    
    my_logout_btn = new JButton("Logout");
    my_logout_btn.addActionListener(this);
    my_spring_layout.putConstraint(SpringLayout.NORTH, my_logout_btn, 10, SpringLayout.SOUTH, my_view_my_jobs_btn);
    my_spring_layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, my_logout_btn, 0, SpringLayout.HORIZONTAL_CENTER, this);
    add(my_logout_btn);
  }

  /**
   * @param args
   */
  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setContentPane(new UserPanel(new MainWindow(new Controller()), new User("","",Role.STAFF_MEMBER)));
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.setVisible(true);

  }

  @Override
  public void actionPerformed(ActionEvent e) {
    if(e.getSource() == my_logout_btn){
      my_parent.doLogout();
    } else if(e.getSource() == my_view_calendar_btn){
      my_parent.showCalendar();
    } else if(e.getSource() == my_view_volunteers_btn){
      my_parent.showVolunteerList();
    } else if(e.getSource() == my_view_blackballed_btn){
      my_parent.showBlackballedList();
    } else if(e.getSource() == my_view_all_jobs_btn){
      my_parent.showAllJobsList();
    } else if(e.getSource() == my_view_pending_jobs_btn){
      my_parent.showPendingJobsList();
    } else if(e.getSource() == my_view_my_jobs_btn){
      my_parent.showMyJobsList();
    } else if(e.getSource() == my_submit_job_btn){
      my_parent.showJobForm();
    }
  }

}
